import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/utils/xindong_text_util.dart';

// 透明加载窗
class XinDongLoadingTransparent {
  static show() {
    SmartDialog.showLoading(
      // 返回不隐藏
      backDismiss: false,
      // false点击不隐藏
      clickMaskDismiss: false,
      // 显示动画
      useAnimation: true,
      maskColor: Colors.transparent,
      builder: (BuildContext context) {
        // 添加长按隐藏
        return GestureDetector(
          onLongPress: () => hide(),
          behavior: HitTestBehavior.opaque,
          child: Container(alignment: Alignment.center, color: DZColors.clear),
        );
      },
    );
  }

  static hide() {
    SmartDialog.dismiss(status: SmartStatus.loading);
  }
}

// 加载显示文本Dialog
class XinDongLoadingText {
  static show(String? text) {
    if (TextUtil.isEmpty(text)) {
      return;
    }
    SmartDialog.showLoading(
      msg: text ?? "",
      // 不显示动画，所以文字更换不闪动
      useAnimation: false,
      builder: (BuildContext context) {
        // 添加长按隐藏
        return GestureDetector(
          onLongPress: () => hide(),
          behavior: HitTestBehavior.opaque,
          child: Container(
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const SizedBox(
                  width: 40,
                  height: 40,
                  child: XinDongLoadingCenter(),
                ),
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Text(
                    text ?? "",
                    textAlign: TextAlign.center,
                    style: const TextStyle(fontSize: 16, fontWeight: FontWeight.normal, color: DZColors.mainColor, height: 1.2),
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }

  static hide() {
    SmartDialog.dismiss(status: SmartStatus.loading);
  }
}

// 加载进度Dialog
class XinDongLoadingProgress {
  static show(double? progress, {String? doneText, int doneMilliseconds = 2000}) {
    if (progress == null) {
      return;
    }
    if (progress >= 1.0 && !TextUtil.isEmpty(doneText)) {
      Future.delayed(Duration(milliseconds: doneMilliseconds)).whenComplete(() => hide());
    }
    SmartDialog.showLoading(
      // 不显示动画，所以文字更换不闪动
      useAnimation: false,
      builder: (BuildContext context) {
        // 添加长按隐藏
        return GestureDetector(
          onLongPress: () => hide(),
          behavior: HitTestBehavior.opaque,
          child: Container(
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const SizedBox(
                  width: 40,
                  height: 40,
                  child: XinDongLoadingCenter(),
                ),
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Text(
                    (progress >= 1.0 && !TextUtil.isEmpty(doneText)) ? doneText! : "${(progress * 100).toStringAsFixed(0)}%",
                    textAlign: TextAlign.center,
                    style: const TextStyle(fontSize: 16, fontWeight: FontWeight.normal, color: DZColors.mainColor, height: 1.2),
                  ),
                )
              ],
            ),
          ),
        );
      },
    );
  }

  static hide() {
    SmartDialog.dismiss(status: SmartStatus.loading);
  }
}

// 转圈圈加载
class XinDongLoadingCenter extends StatelessWidget {
  final double size;
  final double strokeWidth;
  final Color color;

  const XinDongLoadingCenter({Key? key, this.size = 30, this.strokeWidth = 2.5, this.color = DZColors.mainColor}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: SizedBox(
        width: size,
        height: size,
        child: CircularProgressIndicator(
          strokeWidth: strokeWidth,
          valueColor: AlwaysStoppedAnimation<Color>(color),
        ),
      ),
    );
  }
}
